package com.study.demo;



import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * 模拟登陆窗口
 * @author Administrator
 *
 */
public class LoginDemo extends Application{
	public static void main(String[] args) {
		launch(args);
	}
	public void start(Stage stage) throws Exception {
		stage.show();

		GridPane grid = new GridPane();

		Label accountLabel = new Label("账号:");
		Label passwdLabel = new Label("密码:");

		TextField accountField = new TextField();
		accountField.setTooltip(new Tooltip("请输入账号"));
		accountField.setPromptText("请输入账号");
		//accountField.setFocusTraversable(false);

		PasswordField passwdField = new PasswordField();
		passwdField.setTooltip(new Tooltip("请输入密码"));
		passwdField.setPromptText("请输入密码");
		//passwdField.setFocusTraversable(false);

		Button loginBtn= new Button("登 陆");
		Button cancelBtn = new Button("取 消");

		accountField.setUserData("admin");
		passwdField.setUserData("123");


		grid.add(accountLabel, 0, 0);
		grid.add(passwdLabel, 0, 1);

		grid.add(accountField, 1, 0);
		grid.add(passwdField, 1, 1);

		grid.add(loginBtn, 0, 2);
		grid.add(cancelBtn, 1, 2);

		grid.setMargin(cancelBtn, new Insets(0, 0, 0, 120));

		grid.setAlignment(Pos.CENTER);
		grid.setHgap(10);
		grid.setVgap(10);
		stage.setScene(new Scene(grid,300,200));
		stage.setResizable(false);

		loginBtn.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				if(accountField.getUserData().toString().equals(accountField.getText()) && passwdField.getUserData().toString().equals(passwdField.getText())){
					System.out.println("登陆成功...");
					MainWindow main = new MainWindow();
					//关闭登陆界面
					stage.close();


				}else{
					System.out.println("登陆失败...");
					//登陆失败后 窗口闪动一下
					FadeTransition fade = new FadeTransition(Duration.seconds(1),grid);
					fade.setDuration(Duration.seconds(0.1));
					//fade.setNode(grid);   通过setNode()方法也可以设置对应的组件
					fade.setFromValue(0);
					fade.setToValue(1.0);
					fade.play();
				}

			}
		});
	};
}


class MainWindow{

	public MainWindow(){
		Stage stage = new Stage();
		stage.show();
		stage.setTitle("主界面");
	}



}
